package www.hszw.search.palant.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.hszw.search.base.message.enums.ResponseCode;
import www.hszw.search.base.message.response.ResponseResult;
import www.hszw.search.base.service.BaseElasticService;
import www.hszw.search.base.util.StringUtils;
import www.hszw.search.domain.service.GoodEsService;
import www.hszw.search.palant.utils.ElasticUtil;
import www.hszw.search.palant.vo.QueryVo;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * jd商品查询
 */
@Slf4j
@RequestMapping("/search/good")
@RestController
public class SearchController {

    @Autowired
    BaseElasticService baseElasticService;

    private static final String GOODSIDX = "jdgoods";

    @GetMapping(value = "/query")
    public ResponseResult get(String name){
        ResponseResult response = getResponseResult();
        if(StringUtils.isBlank(name)){
            response.setCode(ResponseCode.PARAM_ERROR_CODE.getCode());
            response.setMsg("参数不能为空");
            response.setStatus(false);
            return response;
        }
        QueryVo queryVo = getQueryVo(name);
        try {
            Map<String,Object> params = queryVo.getQuery().get("match");
            Set<String> keys = params.keySet();
            MatchQueryBuilder queryBuilders=null;
            for(String ke:keys){
                queryBuilders = QueryBuilders.matchQuery(ke, params.get(ke));
            }
            if(null!=queryBuilders){
                SearchSourceBuilder searchSourceBuilder = ElasticUtil.initSearchSourceBuilder(queryBuilders);
                List<JSONObject> data = baseElasticService.search(GOODSIDX,searchSourceBuilder);
                response.setData(data);
            }
        } catch (Exception e) {
            response.setCode(ResponseCode.ERROR.getCode());
            response.setMsg("服务忙，请稍后再试");
            response.setStatus(false);
            log.error("查询数据异常，metadataVo={},异常信息={}", queryVo.toString(),e.getMessage());
        }
        return response;
    }

    public ResponseResult getResponseResult(){
        return new ResponseResult();
    }

    /**
     * 构建查询实体
     * @param name
     * @return
     */
    private QueryVo getQueryVo(String name){
        QueryVo queryVo = new QueryVo();
        queryVo.setIdxName(GOODSIDX);

        Map<String,Object> param = Maps.newHashMap();
        param.put("goodName",name);

        Map<String,Map<String,Object>> matchMap = Maps.newHashMap();
        matchMap.put("match",param);

        queryVo.setQuery(matchMap);

        return queryVo;
    }

    @Autowired
    GoodEsService goodEsService;

    @GetMapping(value = "/queryGood")
    public ResponseResult queryGood(String name){
        ResponseResult response = getResponseResult();
        goodEsService.queryGood();
        return response;
    }

}
